/**
 * Created by Oliver Huynh on 1/16/14.
 */
var select = {
  init : function(evt)
  {
    var appcontent = document.getElementById("appcontent");   // browser
    if (appcontent)
    {
      appcontent.addEventListener("load", select.contentLoadHandler, true);
    }
  },
  contentLoadHandler : function(evt)
  {
    var doc = evt.originalTarget;
    if (doc instanceof HTMLDocument)
    {
      // register mouse handlers
      // we register them with content documents only because we
      // do not want the events fired when user click on menues, etc.
      doc.addEventListener("click", select.clickHandler, true);
    }
  },
  clickHandler : function(evt)
  {
    if (2 == evt.detail)
    {
      // select.lookupSelected()
      select.mouseX = evt.screenX;
      select.mouseY = evt.screenY;

      if (select.isDoubleClick)
      {
        select.lookup(evt);
      }
    }
    else if (1 == evt.detail)
    {
      // set for select.lookupSelected()
      select.mouseX = evt.screenX;
      select.mouseY = evt.screenY;

      if (select.isSelect)
      {
        select.lookup(evt);
      }
    }

    //gd12debug.log("gd12.clickHandler ends");
  },

  lookup : function(evt)
  {
    var myWin = evt.originalTarget.ownerDocument.defaultView;

    select.mouseX = evt.screenX;
    select.mouseY = evt.screenY;

    select.lookupInWin(myWin);
  },

  lookupSelected : function()
  {
    var myWin = window.content;
    if (myWin)
      select.lookupInWin(myWin);
  },

  lookupInWin : function(myWin)
  {
    if (myWin.location.protocol == "https:")
      select.protocol = "https:";
    else
      select.protocol = "http:";

    var selected = myWin.getSelection();

    if (selected)
    {
      var word = selected.toString();

      if (word && word.length > 0)
      {
        //dicInline.translate(word, select.showPopup);
        select.word = word;
      }
    }
  },

  showPopup: function()
  {
    //select.popup.open(select.mouseX, select.mouseY + 18);
    console.log(word);
  },

  mouseX : 0,
  mouseY : 0,
  word : null,
  isSelect : false,
  isDoubleClick : true

};

window.addEventListener("load", select.init, false);
